import axios from "axios";
import useStore from "@/store/index";
import router from "../router"
import {ElMessage} from "element-plus";

const store = useStore()



axios.defaults.baseURL = '/api/decision'
axios.defaults.timeout = 30000

axios.interceptors.request.use(config =>{
    if (store.token) {
        config.headers.Authorization = `Bearer ${store.token}`
    }
    return config
})

axios.interceptors.response.use(response =>{
    return response
},async error => {
    console.log(error.response)
    if (error.response.status === 401) {
        store.token = ''
        await router.push('/login')
    }
    if (error.response.data) {
        ElMessage.error(error.response.data.message)
    }
    return Promise.reject(error)
})

export default axios
